/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.server;

import ftpcombiner.ApplicationController;
import ftpcombiner.server.serverconnection.File;
import ftpcombiner.server.serverconnection.Folder;
import ftpcombiner.server.serverconnection.ServerItem;
import it.sauronsoftware.ftp4j.*;
import java.io.IOException;
import java.net.SocketException;

/**
 *
 * @author Ben
 */
public class ServerConnection extends FTPClient {

    String server, username, password;
    FTPFile[] currFiles;

    public ServerConnection(String server, String username, String password) {
        super();
        this.server = server;
        this.username = username;
        this.password = password;

    }

    public ServerItem[] getFiles() {
        FTPFile[] files = currFiles;
        ServerItem[] items = new ServerItem[files.length];
        for (int i = 0; i < files.length; i++) {
            if (files[i].getType() == FTPFile.TYPE_DIRECTORY) {
                items[i] = new Folder(this, files[i]);
            } else {
                items[i] = new File(this, files[i]);
            }
        }
        return items;
    }

    public synchronized void connectAndLogin() {
        try {
            connect(server);
        } catch (IllegalStateException | IOException | FTPException ex) {
            System.err.println(ex);
        } catch (FTPIllegalReplyException ex) {
            System.err.println("Bad Reply: " + ex);
        }
        try {
            login(username, password);
        } catch (IllegalStateException | IOException | FTPException ex) {
            System.err.println(ex);
        } catch (FTPIllegalReplyException ex) {
            System.err.println("Bad Reply: " + ex);
        }
        updateCurrentFiles();
        setAutoNoopTimeout(35000);
    }

    @Override
    public String getHost() {
        return server;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public synchronized void updateCurrentFiles() {
        try {
            currFiles = list();
        } catch (SocketException e) {
            ApplicationController.getInstance().setAppStatus("Connection to server lost, trying to reconnect...", true);
            connectAndLogin();
        } catch (IllegalStateException | IOException | FTPIllegalReplyException | FTPException | FTPDataTransferException | FTPAbortedException | FTPListParseException ex) {
            ex.printStackTrace();
        }
    }
//    @Override
//    public void download(String remoteFileName, File localFile, FTPDataTransferListener listener) throws IllegalStateException, FileNotFoundException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException {
//        super.download(remoteFileName, localFile, listener);
//    }
}
